package com.hui.service;

import com.hui.entity.*;
import com.hui.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/08/11:14
 * @Description:
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> getRoleByCondition(Role role) {
        return roleMapper.getRoleByCondition(role);
    }

    @Override
    public void saveRole(Role role) {
        // 补全信息
        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);
        role.setCreatedBy("system");
        role.setUpdatedBy("system");
        // 调用Dao层
        roleMapper.saveRole(role);
    }

    @Override
    public void updateRole(Role role) {
        // 补全信息
        role.setUpdatedTime(new Date());
        role.setUpdatedBy("system");
        // 调用Dao层
        roleMapper.updateRole(role);
    }

    @Override
    public List<Integer> getMenuByRoleId(Integer roleId) {
        return roleMapper.getMenuByRoleId(roleId);
    }

    //  为角色分配菜单列表
    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {
        Integer roleId = roleMenuVo.getRoleId();
        // 删除原有的菜单关联信息
        roleMapper.deleteMenuByRoleId(roleId);
        // 分配菜单,遍历菜单Id，单个添加
        Date date = new Date();
        for (Integer menuId : roleMenuVo.getMenuIdList()) {
            // 封装&补全信息
            Role_menu_relation relation = new Role_menu_relation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            relation.setCreatedTime(date);
            relation.setUpdatedTime(date);
            relation.setCreatedBy("system");
            relation.setUpdatedby("system");
            // 调用dao层，执行添加操作
            roleMapper.insertRoleMenuRelation(relation);
        }
    }

    @Override
    public void deleteRole(Integer id) {
        // 删除原有的菜单关联信息
        roleMapper.deleteMenuByRoleId(id);
        // 删除角色
        roleMapper.deleteRoleById(id);
    }

    @Override
    public List<ResourceCategory> getResourceListByRoleId(Integer roleId) {
        // 获取当前角色的资源分类信息
        List<ResourceCategory> categoryList = roleMapper.getResourceCategoryByRoleId(roleId);
        System.out.println(categoryList);
        // 获取当前角色的资源信息
        List<Resource> resourceList = roleMapper.getResourceByRoleId(roleId);
        System.out.println(categoryList);
        if (categoryList == null || resourceList == null) {
            return null;
        }
        // 将资源信息封装到资源分类中
        for (ResourceCategory category : categoryList) {
            // 初始化 resourceList 集合
            category.setResourceList(new ArrayList<>());
            for (Resource resource : resourceList) {
                // 如果当前资源属于该分类下，就添加到 resourceList 集合中
                if (resource.getCategoryId() == category.getId()) {
                    category.getResourceList().add(resource);
                }
            }
        }
        return categoryList;
    }

    @Override
    public void roleContextResource(RoleResourceVo resourceVo) {
        // 删除原有的角色与资源关联
        roleMapper.deleteResourceByRoleId(resourceVo.getRoleId());
        // 添加角色与资源关联
        Date date = new Date();
        if (resourceVo.getResourceIdList() != null) {
            for (Integer resourceId : resourceVo.getResourceIdList()) {
                Role_resource_relation relation = new Role_resource_relation(null, resourceId, resourceVo.getRoleId(), date, date, "system", "system");
                roleMapper.insertRoleResourceRelation(relation);
            }
        }
    }
}
